package com.wali.modules.bil.control;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wali.common.ccp.exception.CcpException;
import com.wali.common.ccp.execl.CcpExcelReport;
import com.wali.common.ccp.execl.CcpExcelUtils;
import com.wali.common.ccp.page.CcpPagination;
import com.wali.common.enums.ErrorCodeEnum;
import com.wali.common.web.BaseController;
import com.wali.modules.bil.bo.BilExpressBO;
import com.wali.modules.bil.bo.BilGoodsBO;
import com.wali.modules.bil.enums.BilExpressEnums;
import com.wali.modules.bil.service.BilExpressService;
import com.wali.modules.bil.vo.BilExpressVO;
import com.wali.modules.bil.vo.BilGoodsVO;
import com.wali.modules.system.enums.RoleTypeEnum;
import com.wali.modules.system.vo.SysUserVO;
import com.wali.modules.usr.bo.UsrReceiverBO;
import com.wali.modules.usr.bo.UsrSenderBO;
import com.wali.modules.usr.service.UsrReceiverService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author walixu
 * @ClassName com.wali.modules.bil.control
 * @Description ${todo}
 * @email wali.xu@kpmg.com
 * @date 20/01/2019 8:01 下午
 */
@Controller
@RequestMapping(value = {"/bil", "/bill"})
public class BilExpressController extends BaseController {

    @Autowired
    private BilExpressService bilExpressService;

    @Autowired
    private UsrReceiverService usrReceiverService;

    @Value("${upload.path}")
    private String uploadPath;

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation(uploadPath);
        return factory.createMultipartConfig();
    }

    /**
     * @param bilExpressBO
     * @param usrReceiverBO
     * @param usrSenderBO
     * @return
     * @throws
     * @Title saveBilExpress
     * @Description 新增运单信息
     */
    @RequestMapping(value = "/saveBilExpress", method = RequestMethod.POST)
    public ResponseEntity<String> saveBilExpress(BilExpressBO bilExpressBO, UsrReceiverBO usrReceiverBO, UsrSenderBO usrSenderBO, HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            logger.info("saveBilExpress bilExpressBO " + bilExpressBO.toString());
            logger.info("saveBilExpress usrReceiverBO " + usrReceiverBO.toString());
            logger.info("saveBilExpress usrSenderBO " + usrSenderBO.toString());

            HttpSession session = request.getSession(false);
            SysUserVO sysUser = (SysUserVO) session.getAttribute("user");

            // 获取物品信息
            String bilGoods = request.getParameter("bilGoods");
            List<BilGoodsBO> bilGoodsList = JSONArray.parseArray(bilGoods, BilGoodsBO.class);
            logger.info("saveBilExpress bilGoodsList " + bilGoodsList);

            // 保存运单信息
            bilExpressBO.setUserId(sysUser.getId());
            bilExpressService.saveBilExpress(bilExpressBO, usrReceiverBO, usrSenderBO, bilGoodsList);

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title updateBilExpress
     * @Description 更新订单消息
     */
    @RequestMapping(value = "/updateBilExpress", method = RequestMethod.POST)
    public ResponseEntity<String> updateBilExpress(BilExpressBO bilExpressBO, UsrReceiverBO usrReceiverBO, UsrSenderBO usrSenderBO, HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            logger.info("updateBilExpress bilExpressBO " + bilExpressBO.toString());
            logger.info("updateBilExpress usrReceiverBO " + usrReceiverBO.toString());
            logger.info("updateBilExpress usrSenderBO " + usrSenderBO.toString());

            HttpSession session = request.getSession(false);
            SysUserVO sysUser = (SysUserVO) session.getAttribute("user");

            // 获取物品信息
            String bilGoods = request.getParameter("bilGoods");
            List<BilGoodsBO> bilGoodsList = JSONArray.parseArray(bilGoods, BilGoodsBO.class);
            logger.info("updateBilExpress bilGoodsList " + bilGoodsList);

            // 保存运单信息
            if (sysUser.getRoleType() == RoleTypeEnum.NORMAL_USER_TYPE.getType()) {// 如果是普通用户，需要设置userId，不然会有查到不是自己运单的风险
                bilExpressBO.setUserId(sysUser.getId());
            }
            bilExpressService.updateBilExpress(bilExpressBO, usrReceiverBO, usrSenderBO, bilGoodsList);

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title batchUpdateBilExpress
     * @Description 批量更新运单状态
     */
    @RequestMapping(value = "/batchUpdateBilExpressState")
    public ResponseEntity<String> batchUpdateBilExpressState(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            String waybillNos = request.getParameter("waybillNos");
            int state = Integer.parseInt(request.getParameter("state"));

            if (state != 0) {
                bilExpressService.batchUpdateBilExpressState(waybillNos, state);
                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
            }
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }


    /**
     * @param
     * @return
     * @throws
     * @Title uploadIdCardFront
     * @Description 上传身份证正面
     */
    @RequestMapping(value = "/uploadIdCardFront")
    public ResponseEntity<String> uploadIdCardFront(@RequestParam("idCardFront$") MultipartFile file) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            logger.info("uploadIdCardFront files size = " + file.getSize());
            logger.info("uploadIdCardFront files getOriginalFilename = " + file.getOriginalFilename());
            logger.info("uploadIdCardFront files getName = " + file.getName());

            String originalFilename = file.getOriginalFilename();

            String filename = System.currentTimeMillis() + "";
            String newFilename = filename + originalFilename.substring(originalFilename.lastIndexOf("."));

            String path = uploadPath + File.separator + "idCardFront" + File.separator + newFilename;
            File idCardFront = new File(path);
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(idCardFront));
            os.write(file.getBytes());
            os.flush();
            os.close();

            return getResponseEntity(ErrorCodeEnum.SUCCESS_CODE.getCode(), ErrorCodeEnum.SUCCESS_CODE.getMessage(), newFilename);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title uploadIdCardBack
     * @Description 上传身份证反面
     */
    @RequestMapping(value = "/uploadIdCardBack")
    public ResponseEntity<String> uploadIdCardBack(@RequestParam("idCardBack$") MultipartFile file) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            logger.info("uploadIdCardBack files size = " + file.getSize());
            logger.info("uploadIdCardBack files getOriginalFilename = " + file.getOriginalFilename());
            logger.info("uploadIdCardBack files getName = " + file.getName());

            String originalFilename = file.getOriginalFilename();

            String filename = System.currentTimeMillis() + "";
            String newFilename = filename + originalFilename.substring(originalFilename.lastIndexOf("."));

            String path = uploadPath + File.separator + "idCardBack" + File.separator + newFilename;
            File idCardBack = new File(path);
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(idCardBack));
            os.write(file.getBytes());
            os.flush();
            os.close();

            return getResponseEntity(ErrorCodeEnum.SUCCESS_CODE.getCode(), ErrorCodeEnum.SUCCESS_CODE.getMessage(), newFilename);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 预览身份证正面
     */
    @RequestMapping(value = "/previewIdCardFront")
    public void previewIdCardFront(HttpServletRequest request, HttpServletResponse response) {
        try {
            String idCardFront = request.getParameter("idCardFront");
            String path = uploadPath + File.separator + "idCardFront" + File.separator + idCardFront;
            previewImg(response, path);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private void previewImg(HttpServletResponse response, String path) throws IOException {
        ServletOutputStream out = response.getOutputStream();
        if (!new File(path).exists()) {
            path = uploadPath + File.separator + "idCardBack" + File.separator + "default.png";
        }

        InputStream in = new FileInputStream(path);
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 预览身份证反面
     */
    @RequestMapping(value = "/previewIdCardBack")
    public void previewIdCardBack(HttpServletRequest request, HttpServletResponse response) {
        try {
            String idCardBack = request.getParameter("idCardBack");
            String path = uploadPath + File.separator + "idCardBack" + File.separator + idCardBack;

            previewImg(response, path);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 删除图片
     */
    @RequestMapping(value = "/deleteIdCardFront")
    public ResponseEntity<String> deleteIdCardFront(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            String idCardFront = request.getParameter("idCardFront");
            String path = uploadPath + File.separator + "idCardFront" + File.separator + idCardFront;

            UsrReceiverBO usrReceiverBO = new UsrReceiverBO();
            usrReceiverBO.setIdCardFront(idCardFront);
            usrReceiverService.deleteUsrReceiverIdCardFront(usrReceiverBO);

            File file = new File(path);
            file.delete();

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 删除图片
     */
    @RequestMapping(value = "/deleteIdCardBack")
    public ResponseEntity<String> deleteIdCardBack(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            String idCardBack = request.getParameter("idCardBack");
            String path = uploadPath + File.separator + "idCardBack" + File.separator + idCardBack;

            UsrReceiverBO usrReceiverBO = new UsrReceiverBO();
            usrReceiverBO.setIdCardBack(idCardBack);
            usrReceiverService.deleteUsrReceiverIdCardBack(usrReceiverBO);

            File file = new File(path);
            file.delete();

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title listBilExpress
     * @Description 查询符合条件的运单信息
     */
    @RequestMapping(value = "/listBilExpress")
    public ResponseEntity<String> listBilExpress(HttpServletRequest request, BilExpressBO bilExpressBO, @RequestParam("page") int page, @RequestParam("limit") int limit) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            HttpSession session = request.getSession(false);
            SysUserVO sysUser = (SysUserVO) session.getAttribute("user");

            // 先注释本行，可以查询所有的订单
            if (sysUser.getRoleType() == RoleTypeEnum.NORMAL_USER_TYPE.getType()) {// 普通用户查询自己的运单
                bilExpressBO.setUserId(sysUser.getId());
            } else if (sysUser.getRoleType() == RoleTypeEnum.STORE_ADMIN_USER_TYPE.getType()) {// 分店管理员查询自己分店的运单
                bilExpressBO.setStoreCode(sysUser.getStoreCode());
            }

            String states = request.getParameter("states");
            List<Integer> statesList = null;
            if (StringUtils.isNotBlank(states)) {
                statesList = new ArrayList<>();
                String[] statesArr = states.split(",");
                for (String state : statesArr) {
                    statesList.add(Integer.parseInt(state));
                }
            }

            CcpPagination<BilExpressVO> pagination = bilExpressService.listBilExpress(bilExpressBO, statesList, page, limit);
            return getResponseEntity(pagination);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title listBilExpressByIds
     * @Description 根据id查询符合条件的运单信息列表
     */
    @RequestMapping(value = "/listBilExpressByIds")
    public ResponseEntity<String> listBilExpressByIds(HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            Map<String, Object> result = new HashMap<>();
            List<BilExpressVO> expressList = new ArrayList<>();
            List<BilGoodsVO> goodsList = new ArrayList<>();

            String ids = request.getParameter("ids");
            if (ids.startsWith("TDC")) {
                BilExpressBO bilExpress = new BilExpressBO();
                bilExpress.setWaybillNo(ids);
                BilExpressVO bilExpressVO = bilExpressService.getBilExpress(bilExpress);
                expressList = new ArrayList<>();
                expressList.add(bilExpressVO);

                BilGoodsBO bilGoods = new BilGoodsBO();
                bilGoods.setWaybillNo(ids);
                goodsList = bilExpressService.listBilGoods(bilGoods);
            } else {
                HttpSession session = request.getSession(false);
                SysUserVO sysUser = (SysUserVO) session.getAttribute("user");

                List<Integer> idsList = new ArrayList<>();
                String[] idsArr = ids.split(",");
                for (String id : idsArr) {
                    idsList.add(Integer.parseInt(id));
                }
                if (idsList.size() != 0) {
                    expressList = bilExpressService.listBilExpressByIds(idsList, sysUser.getStoreCode());
                    goodsList = bilExpressService.listBilGoodsByExpressIds(idsList);
                }
            }

            result.put("expressList", expressList);
            result.put("goodsList", goodsList);

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();

            return getResponseEntity(code, msg, result);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 统计符合条件的运单信息数量
     */
    @RequestMapping(value = "/countBilExpress")
    public ResponseEntity<String> countBilExpress(HttpServletRequest request, BilExpressBO bilExpressBO) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            HttpSession session = request.getSession(false);
            SysUserVO sysUser = (SysUserVO) session.getAttribute("user");

            if (sysUser.getRoleType() == RoleTypeEnum.NORMAL_USER_TYPE.getType()) {
                bilExpressBO.setUserId(sysUser.getId());
            } else if (sysUser.getRoleType() == RoleTypeEnum.STORE_ADMIN_USER_TYPE.getType()) {// 分店管理员查询自己分店的运单
                bilExpressBO.setStoreCode(sysUser.getStoreCode());
            }

            String states = request.getParameter("states");
            List<Integer> statesList = null;
            if (StringUtils.isNotBlank(states)) {
                statesList = new ArrayList<>();
                String[] statesArr = states.split(",");
                for (String state : statesArr) {
                    statesList.add(Integer.parseInt(state));
                }
            }

            int count = bilExpressService.countBilExpress(bilExpressBO, statesList);

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
            return getResponseEntity(code, count);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 导出符合条件的运单信息
     */
    @RequestMapping(value = "/exportBilExpress")
    public void exportBilExpress(BilExpressBO bilExpressBO, HttpServletRequest request, HttpServletResponse response) {
        try {
            String states = request.getParameter("states");
            List<Integer> statesList = null;
            if (StringUtils.isNotBlank(states)) {
                statesList = new ArrayList<>();
                String[] statesArr = states.split(",");
                for (String state : statesArr) {
                    statesList.add(Integer.parseInt(state));
                }
            }
            CcpExcelReport report = bilExpressService.exportBilExpress(bilExpressBO, statesList);
            writeExecl(response, report);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 导出符合条件的运单信息
     */
    @RequestMapping(value = "/exportBilExpressTo")
    public void exportBilExpressTo(BilExpressBO bilExpressBO, HttpServletResponse response) {
        try {
            CcpExcelReport report = bilExpressService.exportBilExpressTo(bilExpressBO);
            writeExecl(response, report);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * @param
     * @return
     * @throws
     * @Title
     * @Description 导入运单号与中通快递映射关联
     */
    @RequestMapping(value = "/importBilExpressNo")
    public ResponseEntity<String> importBilExpressNo(@RequestParam("bilExpressNoFile") MultipartFile file, HttpServletRequest request) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            if (file.getSize() > 5000000) {// 不能超过5M
                throw new CcpException(BilExpressEnums.FILE_TOO_LARGE);
            }

            CcpExcelUtils excelUtils = new CcpExcelUtils(file, -1, 0);
            List<BilExpressVO> bilExpressList = excelUtils.getDataList(BilExpressVO.class);
            if (bilExpressList.size() > 1000) {
                throw new CcpException(BilExpressEnums.DATA_TOO_MUCH);
            }

            int count = bilExpressService.batchUpdateBilExpress(bilExpressList);

            JSONObject result = new JSONObject();
            result.put("total", bilExpressList.size());
            result.put("count", count);

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
            return getResponseEntity(code, msg, result);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title getBilExpress
     * @Description 查询指定的运单详细信息
     */
    @RequestMapping(value = "/getBilExpress")
    public ResponseEntity<String> getBilExpress(HttpServletRequest request, BilExpressBO bilExpress) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            HttpSession session = request.getSession(false);
            SysUserVO sysUser = (SysUserVO) session.getAttribute("user");

            if (sysUser.getRoleType() == RoleTypeEnum.NORMAL_USER_TYPE.getType()) {
                bilExpress.setUserId(sysUser.getId());
            } else if (sysUser.getRoleType() == RoleTypeEnum.STORE_ADMIN_USER_TYPE.getType()) {
                bilExpress.setStoreCode(sysUser.getStoreCode());
            }

            BilExpressVO bilExpressVO = bilExpressService.getBilExpress(bilExpress);

            if (bilExpressVO == null) {
                code = BilExpressEnums.BILEXPRESSNO_NOT_EXIST.getCode();
                msg = BilExpressEnums.BILEXPRESSNO_NOT_EXIST.getMessage();
            } else {
                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
            }

            return getResponseEntity(code, msg, bilExpressVO);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title listBilGoods
     * @Description 查询指定运单的物品信息
     */
    @RequestMapping(value = "/listBilGoods")
    public ResponseEntity<String> listBilGoods(BilGoodsBO bilGoods) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            List<BilGoodsVO> bilGoodsList = bilExpressService.listBilGoods(bilGoods);

            code = ErrorCodeEnum.SUCCESS_CODE.getCode();
            msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
            return getResponseEntity(code, msg, bilGoodsList);
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }

    /**
     * @param
     * @return
     * @throws
     * @Title deleteBilExpress
     * @Description 删除运单信息
     */
    @RequestMapping(value = "/deleteBilExpress")
    public ResponseEntity<String> deleteBilExpress(BilExpressBO bilExpress) {
        int code = ErrorCodeEnum.ERROR_INTERNAL_CODE.getCode();
        String msg = ErrorCodeEnum.ERROR_INTERNAL_CODE.getMessage();

        try {
            if (StringUtils.isBlank(bilExpress.getWaybillNo())) {
                throw new CcpException(ErrorCodeEnum.ERROR_DELETE_CODE);
            }

            int count = bilExpressService.deleteBilExpress(bilExpress);
            if (count > 0) {
                code = ErrorCodeEnum.SUCCESS_CODE.getCode();
                msg = ErrorCodeEnum.SUCCESS_CODE.getMessage();
                return getResponseEntity(code, msg);
            }
        } catch (CcpException e) {
            logger.error(e.getMessage(), e);
            code = e.getException().getCode();
            msg = e.getException().getMessage();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return getResponseEntity(code, msg);
    }
}
